package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.Maintenance;
import cn.zucc.edu.handyzucc.entity.MaintenanceExtend;
import cn.zucc.edu.handyzucc.service.MaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/maintenance")
public class MaintenanceController {
    @Autowired
    private MaintenanceService maintenanceService;

    @RequestMapping(value = "getAllMaintenance", method = RequestMethod.GET)
    private Map<String, Object> getAllMaintenance() {
        Map<String, Object> modelMap = new HashMap<>();
        List<MaintenanceExtend> maintenanceExtends = maintenanceService.getAllMaintenance();
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", maintenanceExtends);
        return modelMap;
    }

    @RequestMapping(value = "getMaintenancesByDorm", method = RequestMethod.POST)
    private Map<String, Object> getMaintenancesByDorm(Integer maintenance_dorm) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Maintenance> maintenances = maintenanceService.getMaintenancesByDorm(maintenance_dorm);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", maintenances);
        return modelMap;
    }

    @RequestMapping(value = "addMaintenance", method = RequestMethod.POST)
    private Map<String, Object> addMaintenance(@RequestBody Maintenance maintenance) {
        Map<String, Object> modelMap = new HashMap<>();
        boolean success = maintenanceService.addMaintenance(maintenance);
        if (success) {
            modelMap.put("code", "0");
            modelMap.put("info", "添加成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "添加失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "deleteMaintenance", method = RequestMethod.POST)
    private Map<String, Object> deleteMaintenance(Integer maintenance_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (maintenanceService.deleteMaintenance(maintenance_id)) {
            modelMap.put("code", "0");
            modelMap.put("info", "删除成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "删除失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "deleteMaintenanceInVue", method = RequestMethod.POST)
    private Map<String, Object> deleteMaintenanceInVue(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (maintenanceService.deleteMaintenance(Integer.parseInt(map.get("maintenance_id").toString()))) {
            modelMap.put("code", "0");
            modelMap.put("info", "删除成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "删除失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "completeMaintenance", method = RequestMethod.POST)
    private Map<String, Object> completeMaintenance(Integer maintenance_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (maintenanceService.completeMaintenance(maintenance_id)) {
            modelMap.put("code", "0");
            modelMap.put("info", "处理成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "处理失败");
        }
        return modelMap;
    }

}
